package controller

import (
	"cooke-home/common/model/base_dto"
	"cooke-home/common/response"
	"cooke-home/task-service/model/dto"
	"cooke-home/task-service/service"

	"github.com/gin-gonic/gin"

	"strconv"
)

type TaskController struct {
	taskService service.TaskService
}

func NewTaskController(taskService service.TaskService) *TaskController {
	return &TaskController{
		taskService: taskService,
	}
}

func (tc *TaskController) Save(c *gin.Context) {
	var req dto.TaskDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.TaskName == "" {
		response.Error(c, "task_name is required", nil)
		return
	}

	if req.Content == "" {
		response.Error(c, "content is required", nil)
		return
	}

	err := tc.taskService.Save(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *TaskController) Delete(c *gin.Context) {
	var req base_dto.BatchDeleteDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	err := tc.taskService.Delete(req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *TaskController) Update(c *gin.Context) {
	var req dto.TaskDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.TaskID == 0 {
		response.Error(c, "id is required", nil)
		return
	}

	err := tc.taskService.Update(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *TaskController) Get(c *gin.Context) {
	idStr, ok := c.GetQuery("id")
	if !ok || idStr == "" || idStr == "0" {
		response.Error(c, "id is required", nil)
		return
	}

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	task, err := tc.taskService.Get(id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", task)
}

func (tc *TaskController) GetList(c *gin.Context) {
	idStr, ok := c.GetQuery("parent_id")
	if !ok || idStr == "" {
		response.Error(c, "id is required", nil)
		return
	}

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	tasks, err := tc.taskService.GetList(id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", tasks)
}

func (tc *TaskController) UpdateCompleteByParentId(c *gin.Context) {
	var req dto.TaskDto
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	err := tc.taskService.UpdateCompleteByParentId(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}

func (tc *TaskController) GetByComplete(c *gin.Context) {
	idStr, ok := c.GetQuery("parent_id")
	if !ok || idStr == "" {
		response.Error(c, "id is required", nil)
		return
	}

	isCompleteStr, ok := c.GetQuery("is_complete")
	if !ok || idStr == "" {
		response.Error(c, "complete is required", nil)
		return
	}

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	isComplete, err := strconv.Atoi(isCompleteStr)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	tasks, err := tc.taskService.GetByComplete(id, isComplete)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", tasks)
}
